home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Softshoe / Lisa's Portable Parts / Data / ConstData.h < prev    next >
Encoding:
Text File  |  2000-06-23  |  3.4 KB  |  105 lines

  1. // ConstData.h
  2.  
  3. #ifndef ConstData_h
  4. #define ConstData_h
  5.  
  6. #ifndef Integers_h
  7. #include "Integers.h"
  8. #endif
  9. #ifndef DebugMessage_h
  10. #include "DebugMessage.h"
  11. #endif
  12. #ifndef Assert_h
  13. #include "Assert.h"
  14. #endif
  15. #ifndef Range_h
  16. #include "Range.h"
  17. #endif
  18. #ifndef ConstArrayOf_h
  19. #include "ConstArrayOf.h"
  20. #endif
  21.  
  22. // The includes above have been flattened so that weak compilers
  23. // can cope better.
  24.  
  25. class Data;
  26.  
  27. /*
  28.    A ConstData is a range of bytes, starting at Start() and
  29.    not including End().  You can't change them.
  30. */
  31.  
  32. class ConstData
  33.   {
  34.     private:
  35.         const uint8 *start;
  36.         const uint8 *end;
  37.     
  38.     public:
  39.         ConstData()        : start(0), end(0)    {}
  40.  
  41.         ConstData( const void *theStart, const void *theEnd )
  42.           : start( static_cast<const uint8 *>( theStart ) ),
  43.              end( static_cast<const uint8 *>( theEnd ) )
  44.           {
  45.             Assert( theEnd >= theStart );
  46.             Assert( theStart != 0 || theEnd == 0 );
  47.           }
  48.         
  49.         ConstData( const void *theStart, uint32 theLength )
  50.           : start( static_cast<const uint8 *>( theStart ) ),
  51.              end( static_cast<const uint8 *>( theStart ) + theLength )
  52.           {
  53.             Assert( theStart != 0 || theLength == 0 );
  54.           }
  55.  
  56.         ConstData( ConstArrayOf<uint8> array )
  57.           : start( array.Start() ),
  58.              end( array.End() )
  59.           {}
  60.         
  61.         const uint8 *Start() const                                        { return start; }
  62.         const uint8 *End() const                                        { return end; }
  63.  
  64.         uint32 Length() const                                            { return uint32( end - start ); }
  65.                 
  66.         uint32 BoundedLength( uint32 bound ) const                { return ( Length() <= bound ) ? Length() : bound; }
  67.  
  68.         bool IsEmpty() const                                                { return start == end; }
  69.         bool Null() const                                                    { return start == 0; }
  70.         
  71.         const uint8& operator[]( uint32 i ) const                    { Assert( i < Length() ); return start[i]; }
  72.         
  73.         const ConstData Head( uint32 position ) const            { Assert( position <= Length() ); return ConstData( start, start+position ); }
  74.         const ConstData Tail( uint32 position ) const            { Assert( position <= Length() ); return ConstData( start+position, end ); }
  75.         const ConstData Middle( uint32 s, uint32 e ) const        { Assert( s <= e );  Assert( e <= Length() ); return ConstData( start+s, start+e ); }
  76.         const ConstData Middle( URange32 r ) const                { Assert( r.End() <= Length() );  return ConstData( start+r.Start(), start+r.End() ); }
  77.         
  78.         void Truncate( uint32 position )                                { Assert( position <= Length() ); end = start + position; }
  79.         void LimitLength( uint32 limit )                                { if ( limit < Length() )  end = start + limit; }
  80.         
  81.         void Shorten( uint32 amount )                                    { Assert( amount <= Length() ); start += amount; }
  82.         void Lengthen( uint32 amount )                                { end += amount; }
  83.         
  84.         bool StartsWith( ConstData ) const;
  85.         bool EndsWith( ConstData ) const;
  86.         bool Contains( ConstData ) const;
  87.         
  88.         bool Contains( const void *p ) const                { return start <= p && p < end; }
  89.         
  90.         const ConstData operator>>( Data ) const;            // returns the remainder, for chaining
  91.   };
  92.  
  93. bool operator==( const ConstData& a, const ConstData& b );
  94. bool operator<( const ConstData& a, const ConstData& b );
  95. bool operator<=( const ConstData& a, const ConstData& b );
  96.         
  97. inline bool operator!=( const ConstData& a, const ConstData& b )        { return !( a == b ); }
  98. inline bool operator>( const ConstData& a, const ConstData& b )        { return !( a <= b ); }
  99. inline bool operator>=( const ConstData& a, const ConstData& b )        { return !( a < b ); }
  100.  
  101. int32 Compare( ConstData a, ConstData b );
  102. int32 CompareReversed( ConstData a, ConstData b );
  103.  
  104. #endif
  105.